package backend;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Answer implements Comparable<Answer> {
	List<PipeNode> firstPipe;
	int pipesCant;
	
	
	public Answer(){
		this.firstPipe= new ArrayList<PipeNode>();
		this.pipesCant=0;
	}
	
	public Answer(int cantPipes){
		this.pipesCant=cantPipes;
	}
	
	public Answer(int pipesCant, List<PipeNode> firstPipe) {
		this.pipesCant = pipesCant;
		this.firstPipe = firstPipe;
	}

	public boolean isBetterThan(Answer o){
		if(o==null){
			return true;
		}
		return compareTo(o)>0;
	}
	
	public int getPipesCant() {
		return pipesCant;
	}
	
	public void add(Pipe pipe, Position pos){
		firstPipe.add(new PipeNode(pipe,pos));
		pipesCant++;
	}
	
	public void removeLast(){
		firstPipe.remove(firstPipe.size()-1);
		pipesCant--;
	}
	
	public Position lastPosition(){
		return firstPipe.get(firstPipe.size()-1).pos;
	}

	@Override
	public int compareTo(Answer o) {
		return pipesCant-o.getPipesCant();
	}
	
	public HashMap<Position, Pipe> getPipes() {
		HashMap<Position, Pipe> out = new HashMap<Position,Pipe>();
		for(PipeNode aux: firstPipe){
			out.put(aux.pos, aux.pipe);
		}
		return out;
	}
	
	protected static class PipeNode{
		Pipe pipe;
		Position pos;
		
		public PipeNode(Pipe pipe, Position pos){
			this.pipe = pipe;
			this.pos = pos;
		}
		
	}
}
